package com.sumainfor.wx.system.maket.service;

import com.google.common.collect.Lists;
import com.sumainfor.common.ConstantsHousing;
import com.sumainfor.wx.system.maket.dao.RdHousingInfoMapper;
import com.sumainfor.wx.system.maket.dao.RdWorderMapper;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 根据房源详表编号模糊查询房源信息
 * <p>
 *
 * @author: zc
 * @create: 2019-08-13 18:05
 **/
@Service
public class RdHousingInfoService implements Serializable {

    @Autowired
    RdHousingInfoMapper rdHousingInfoMapper;

    @Autowired
    RdWorderMapper rdWorderMapper;


    /**
     *  查询房源
     * @author zhoucong
     * @data 2019/8/23
     * @Param
     * @return
     */
    public List<Map<String,Object>> getHousingInfoIdList(Map<String,Object> params){
        return rdHousingInfoMapper.getHousingInfoIdList(params);
    }




    /**
     * 根据房源详表编号查询房源详情
     * @author zhoucong
     * @data 2019/8/23
     * @Param
     * @return
     */
    public List<Map<String,Object>> getOnHousInfoId(Map<String,Object> params){
        List<Map<String,Object>> getOnHousInfoId = rdHousingInfoMapper.getOnHousInfoId(params);
        getOnHousInfoId.stream().map(
                item ->{
                    //处理查询房源特点
                    String inId = MapUtils.getString(item, "InId", "");
                    List<Object> trait = rdWorderMapper.selectDetailList(Lists.newArrayList(inId.split(",")))
                            .stream().map(tar -> tar.get("InName")).collect(Collectors.toList());
                    if (StringUtils.isNotBlank(MapUtils.getString(item, "Towards"))) {
                        trait.add("朝" + item.get("Towards"));
                    } else {
                        trait.add("朝" + MapUtils.getString(item, "Aspect"));
                        item.put("Towards", MapUtils.getString(item, "Aspect"));
                    }
                    item.remove("Aspect");
                    item.put("trait", trait);

                    item.remove("InId");


                    if(StringUtils.isNotBlank(MapUtils.getString(item,"ShowImg"))){
                        //处理图片
                        item.put("HouImg", item.get("ShowImg"));
                        item.remove("ShowImg");
                    }else{
                        item.put("HouImg", ConstantsHousing.DefaultHousingImg);
                    }

                    return item;
                }).collect(Collectors.toList());

        return getOnHousInfoId;
    }





}
